package main;

import island.Island;
import items.Item;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;

import java.awt.Canvas;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.AreaAveragingScaleFilter;
import java.awt.image.FilteredImageSource;

import entity.Entity;

import mob.Character;
import mob.Mob;
import mob.Zombie;

import weapon.Weapon;

public class World {
	public final int BLOCK_SIZE = 16;
	public final int GRAVITY = 1;
	public static final double VX_SLOWLY = 0.9;
	
    public boolean stepState = true;
        
	public static Input input;
	
	public static int k = 0;
//	public static byte[][] level;
	public static ArrayList<Island> islands; 
//	public static ArrayList<Mob> mobs;
	public static ArrayList<Weapon> weapons;	
//	public static ArrayList<Item> items;
	public static Character character;
	
	public ArrayList<Entity> entities;
	
//    public static int count = 0, k1 = 0, k2 = 0;
//    private Image active;
    
	public World()
	{
		clear();
		createLevel();
		
//        Game.pic = new Pictures();
	}
	public void clear()
	{
		weapons = new ArrayList<Weapon>();
		islands = new ArrayList<Island>();
		entities = new ArrayList<Entity>();
	}
	public void createLevel()
	{
//		new Island(0, 3000, 0, 0);
		new Island(-32*BLOCK_SIZE, 0, 0, 0, this);
		
		Random r = new Random();
		for(int q=0;q<100;q++)
		{
			int x = (int) (2000 * r.nextGaussian());
			int y = (int) (1000 * r.nextGaussian());
			int width = 1 + (int) (16 * r.nextDouble());
			int height = 1 + (int) (16 * r.nextDouble());
			
			if(!collideIslands(x, y, width, height))
			{
				new Island(x, y, 0, 0, this, width, height);
			}
			else q--;
		}
		
//		ArrayList<Integer> mobsCoords = ImageParser.coords();
//		character = new Character(mobsCoords.get(0), mobsCoords.get(1));
		character = new Character(0, 0, this);
//		for(int q=2;q<mobsCoords.size();q+=2)
//		{
//			new Mob(mobsCoords.get(q), mobsCoords.get(q+1));
//		}
	}
	public void step()
	{
		for(int q=0;q<islands.size();q++)
		{
			Island island = islands.get(q);
			island.tick();
		}
		for(int q=0;q<entities.size();q++)
		{
			Entity e = entities.get(q);
			if(e.isDeleted())
			{
				entities.remove(q);
				q--;
				continue;
			}
			e.tick();
		}
		for(int q=0;q<entities.size();q++)
		{
			for(int w=q;w<entities.size();w++)
			{
				if(!entities.get(q).isCollide(entities.get(w))) continue;
				entities.get(q).interactOn(entities.get(w));
				entities.get(w).interactOn(entities.get(q));
			}
		}
		
        
        for(int q=0;q<weapons.size();q++)
        {
        	Weapon weapon = weapons.get(q);
        	weapon.tick();
        }
    }
	public boolean collideIslands(double x, double y)
	{
		return collideIslands(x, y, 0, 0);
	}
	public boolean collideIslands(double x, double y, int width, int height)
	{
		boolean c = false;
		for(Island island:islands)
		{
			for(int q=0;q<=width;q++)
			{
				for(int w=0;w<=height;w++)
				{
					try
					{        		
						if( island.blocks
								[q + (int) ((x-island.getX())/BLOCK_SIZE)]
										[w + (int) ((y-island.getY())/BLOCK_SIZE)] 
												!= null)
							c = true;
					}
					catch(Exception ex){}
				}
			}
		}      
		return c;
	}
	
	public void draw(Graphics2D g)
	{       
            g.setColor(Color.BLACK);
            for(Island island:islands)
            {
            	island.draw(g);
            }           
	        for(Entity e:entities)
	        {
	            e.draw(g);
	        }            
            for(Weapon wep : weapons)
            {                                
                wep.draw(g);
            }     
            g.setColor(Color.BLUE);
            g.fillOval(-10-Game.x, -10-Game.y, 20, 20);            
	}
}
